<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Canvas Rectangles</title>
    <style>
        canvas {
            border: 1px solid black;
        }
    </style>
</head>

<body>
    <canvas id="myCanvas" width="800" height="600"></canvas>
    <script>
        // 获取 Canvas 元素和绘图上下文
        const canvas = document.getElementById('myCanvas');
        const ctx = canvas.getContext('2d');

        // 定义矩形对象数组
        const objects = [
            { type: 'rect', x: 0, y: 0, width: 100, height: 50, rotation: 0 },
            { type: 'rect', x: 80, y: 51, width: 100, height: 50, rotation: -3 },
        ];

        // 绘制矩形的函数
        function drawRect(rect) {
            // 保存当前的绘图状态
            ctx.save();

            // 将画布原点移动到矩形的左上角点
            ctx.translate(rect.x, rect.y);

            // 将旋转角度从度转换为弧度
            const rotationInRadians = rect.rotation * (Math.PI / 180);

            // 旋转画布
            ctx.rotate(rotationInRadians);

            // 设置填充颜色
            ctx.fillStyle = 'blue';

            // 绘制矩形
            ctx.fillRect(0, 0, rect.width, rect.height);

            // 恢复之前保存的绘图状态
            ctx.restore();
        }

        // 遍历对象数组并绘制每个矩形
        objects.forEach(drawRect);
    </script>
</body>

</html>